/*

This file is part of roomyak.

Copyright (C) 2008 Pixecur Technologies Incorporated (Esotera).
Visit Esotera at http://www.esoteras3.com
 
roomyak is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
 
roomyak is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with roomyak; see the file COPYING.  If not, write to 
Pixecur Technologies, 213-31 Peet Street, St. John's, NL, A1B 3W8. 
 
Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
 
*/

package com.datasentinel.util.data;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;

public class WritableByteArray extends ReadOnlyByteArray
{
	private static final long serialVersionUID = 8593324505295305595L;

	protected ByteArrayOutputStream outputStream;

	protected WritableByteArray(byte[] data)
	{
		this(data, false);
	}

	public WritableByteArray(int size)
	{
		super(size);
		initializeWriterStream();
	}

	protected WritableByteArray(byte[] data, boolean createCopy)
	{
		super(data, createCopy);
		initializeWriterStream();
	}

	protected void initializeWriterStream()
	{
		outputStream = new FixedSizeByteArrayOutputStream(data);
	}

	public void write(int b) throws IOException
	{
		outputStream.write(b);
	}

	public void write(byte b[]) throws IOException
	{
		write(b, 0, b.length);
	}

	public void write(byte b[], int off, int len) throws IOException
	{
		outputStream.write(b, off, len);
	}

	public int getWrittenByteCount()
	{
		return outputStream == null ? 0 : outputStream.size();
	}

	public void reset()
	{
		Arrays.fill(data, (byte)0);
		this.outputStream.reset();
	}
}
